//
//  messageViewController.m
//  ioschishan
//
//  Created by 千翼网络科技 on 2017/7/13.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "messageViewController.h"
#import "messageeTableViewCell.h"
#import "AddressPickerView.h"
#import "FS_MyChangrSexViewController.h"
#import "schoolChangeViewController.h"
#import "AFHTTPRequestOperationManager.h"

#import "STPhotoKitController.h"
#import "UIImagePickerController+ST.h"
#import "UIButton+AddAction.h"
#import "ICN_HeadImageSelView.h"

@interface messageViewController ()<UITableViewDelegate , UITableViewDataSource , AddressPickerViewDelegate ,UITextFieldDelegate,numDelegate , UINavigationControllerDelegate,UIImagePickerControllerDelegate,STPhotoKitDelegate>{
    
    NSArray *titleArray;
    NSMutableArray *messageArray;
    
    UIView *grayView;
    
    int imgMark;
}

@property (nonatomic, strong) ICN_HeadImageSelView *changeHeadImageView;
@property (nonatomic, strong) UIImage *headimage;
@property (nonatomic, strong) UIImage *bgimage;

@property(nonatomic,strong) UIView *dateBgView;
@property(nonatomic,strong) UIDatePicker *myDatePicker;

@property(nonatomic,strong) UIView *bgView;
@property(nonatomic,strong) AddressPickerView * pickerView;

@property(nonatomic,strong) NSMutableArray *dataArray;
@property(nonatomic,strong) NSMutableDictionary *dataDic;
@property(nonatomic,strong) UITableView *personTableView;

@property(nonatomic,strong) UIImageView *bgImg;
@property(nonatomic,strong) UIImageView *headImg;

@property(nonatomic,strong) UITextField *nameTextField;
@property(nonatomic,strong) UITextField *contentTextField;
@property(nonatomic,strong) UITextField *schoolNameTextField;

@end

@implementation messageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self datePicker];
    [self islogin];
}

#pragma mark ----------------------- islogin ---------------------

- (void)islogin{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    hud.alpha = 0.0;
    
    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:login_isLogin
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   
                                   if ([[returnData objectForKey:@"code"]intValue] == 1) {
                                       
                                       [hud removeFromSuperview];
                                       
                                       [self requestData];
                                       
                                   }else{
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:@"登陆过期！请重新登陆" duration:1.0f];
                                       
                                       welcomeViewController *loginCtrl = [[welcomeViewController alloc] init];
                                       UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:loginCtrl];
                                       self.view.window.rootViewController = navCtrl;
                                       self.view.window.backgroundColor = [UIColor whiteColor];
                                       [self.view.window makeKeyAndVisible];
                                   }
                                   
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:1.0f];
                               }];
}

#pragma mark ----------------------- requestData ---------------------

- (void)requestData{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:more_showMyData
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   
                                   if ([[returnData objectForKey:@"code"]intValue] == 1) {
                                       
                                       [hud removeFromSuperview];
                                       
                                       _dataDic = returnData[@"data"];
                                       NSLog(@"\n\n\n===编辑资料===\n\n\n%@",_dataDic);
                                       [self reloadView];
                                       [_personTableView reloadData];
                                       
                                   }else{
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:1.0f];
                                   }
                                   
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:1.0f];
                               }];
}

- (void)reloadView{
    
    if ([_dataDic[@"back_img"] length] > 0) {
        
        NSString *urlN = manage(AP_HOST, _dataDic[@"back_img"]);
        NSURL *urlW = [NSURL URLWithString:urlN];
        [_bgImg sd_setImageWithURL:urlW placeholderImage:[UIImage imageNamed:@"niunai"]];
        
    }
    
    NSString *urlN1 = manage(AP_HOST, _dataDic[@"header_img"]);
    NSURL *urlW1 = [NSURL URLWithString:urlN1];
    [_headImg sd_setImageWithURL:urlW1 placeholderImage:[UIImage imageNamed:@"niunai"]];
    
    _headimage = _headImg.image;
    _bgimage = _bgImg.image;
    
    [messageArray replaceObjectAtIndex:0 withObject:_dataDic[@"user_name"]];
    [messageArray replaceObjectAtIndex:1 withObject:_dataDic[@"school_name"]];
    [messageArray replaceObjectAtIndex:2 withObject:_dataDic[@"content"]];
    [messageArray replaceObjectAtIndex:3 withObject:_dataDic[@"address"]];
    [messageArray replaceObjectAtIndex:4 withObject:_dataDic[@"birthday"]];
    [messageArray replaceObjectAtIndex:5 withObject:_dataDic[@"sex"]];
    
    [_personTableView reloadData];
}

#pragma mark ------------------ init -------------------

- (void)datePicker{
    
    _dateBgView = [[UIView alloc]initWithFrame:CGRectMake(0, scrHeight(), scrWidth(), scrHeight() - screenScale(200) - screenScale(44 * 5))];
    _dateBgView.backgroundColor = getColor(@"ffffff");
    [[[UIApplication sharedApplication]keyWindow] addSubview:_dateBgView];
    
    UIButton *cancelBtn = [ConfigXp buttonWithFrame:CGRectMake(0, 0, screenScale(100), screenScale(44))
                                              title:@"取消"
                                         titleColor:getColor(@"222222")
                                               font:get4sFont(15)
                                    backgroundColor:[UIColor clearColor]
                                              image:nil];
    [cancelBtn addTarget:self action:@selector(dateCancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_dateBgView addSubview:cancelBtn];
    
    UIButton *finishBtn = [ConfigXp buttonWithFrame:CGRectMake(scrWidth() - screenScale(100), 0, screenScale(100), screenScale(44))
                                              title:@"完成"
                                         titleColor:getColor(@"222222")
                                               font:get4sFont(15)
                                    backgroundColor:[UIColor clearColor]
                                              image:nil];
    [finishBtn addTarget:self action:@selector(dateFinishBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_dateBgView addSubview:finishBtn];
    
    ///

    self.myDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, scrHeight(), scrWidth(), scrHeight() - screenScale(200) - screenScale(44 * 6))];
    self.myDatePicker.datePickerMode = UIDatePickerModeDate;
    self.myDatePicker.backgroundColor = [UIColor colorWithRed:239/255.f
                                                        green:239/255.f
                                                         blue:244.0/255.f
                                                        alpha:1.0];
    self.myDatePicker.hidden = YES;
    [[[UIApplication sharedApplication]keyWindow] addSubview:self.myDatePicker];
}

- (AddressPickerView *)pickerView{
    if (!_pickerView) {
        
        _pickerView = [[AddressPickerView alloc]initWithFrame:CGRectMake(0, scrHeight() , scrWidth(), screenScale(415))];
        _pickerView.delegate = self;
    }
    return _pickerView;
}

- (void)initView{
    
    self.title = @"编辑资料";
    self.view.backgroundColor = getColor(@"f5f5f5");
    titleArray = @[@"昵称",@"学校",@"简介",@"所在地",@"生日",@"性别"];
    messageArray = [[NSMutableArray alloc]initWithObjects:@"大米",@"请选择你的学校",@"介绍一下自己吧",@"辽宁沈阳",@"请选择",@"男", nil];
    
    UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, screenScale(60), screenScale(40))];
    rightBtn.titleEdgeInsets = UIEdgeInsetsMake(0, screenScale(20), 0, screenScale(-20));
    rightBtn.titleLabel.font = get4sFont(14);
    [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
    [rightBtn setTitleColor:getColor(@"4a4a4a") forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItme = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItme;
    
    _personTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, screenScale(200), scrWidth(), scrHeight() - 64 - screenScale(200))];
    _personTableView.backgroundColor = getColor(@"f5f5f5");
    _personTableView.delegate = self;
    _personTableView.dataSource = self;
    _personTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_personTableView];
    
    ///
    
    _bgImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), screenScale(160))];
    _bgImg.userInteractionEnabled = YES;
    _bgImg.image = [UIImage imageNamed:@"3479.jpg_wh1200"];
    [self.view addSubview:_bgImg];
    
    ///
    
    UIButton *headBtn = [ConfigXp buttonWithFrame:CGRectMake(0, screenScale(68), screenScale(72), screenScale(92))
                                            title:nil
                                       titleColor:nil
                                             font:nil
                                  backgroundColor:nil
                                            image:nil];
    [headBtn addTarget:self action:@selector(headBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_bgImg addSubview:headBtn];
    
    _headImg = [[UIImageView alloc]initWithFrame:CGRectMake(screenScale(15), screenScale(0), screenScale(60), screenScale(60))];
    _headImg.layer.masksToBounds = YES;
    _headImg.layer.cornerRadius = screenScale(30);
    _headImg.image = [UIImage imageNamed:@"图层-19"];
    [headBtn addSubview:_headImg];
    
    UILabel *headLabel = [ConfigXp labelWithFrame:CGRectMake(screenScale(10), screenScale(60), screenScale(100), screenScale(28))
                                             text:@"点击更换头像"
                                        textColor:getColor(@"9b9b9b")
                                             font:get4sFont(11)
                                    textAlignment:NSTextAlignmentLeft
                                  backgroundColor:[UIColor clearColor]];
    [headBtn addSubview:headLabel];
    
    ///
    
    UIButton *changeBgBtn = [ConfigXp buttonWithFrame:CGRectMake(screenScale(144), screenScale(65), screenScale(95), screenScale(33))
                                                title:@" 更换背景"
                                           titleColor:getColor(@"ffffff")
                                                 font:get4sFont(12)
                                      backgroundColor:[UIColor blackColor]
                                                image:[UIImage imageNamed:@"modify_background_icon"]];
    changeBgBtn.alpha = 0.6;
    changeBgBtn.layer.borderColor = getColor(@"ffffff").CGColor;
    changeBgBtn.layer.borderWidth = screenScale(1);
    changeBgBtn.layer.cornerRadius = screenScale(3);
    [changeBgBtn addTarget:self action:@selector(changeBgBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_bgImg addSubview:changeBgBtn];
    
    ///
    
    UILabel *messageLabel = [ConfigXp labelWithFrame:CGRectZero
                                                text:@"基本信息"
                                           textColor:getColor(@"4a4a4a")
                                                font:get4sFont(13)
                                       textAlignment:NSTextAlignmentLeft
                                     backgroundColor:[UIColor clearColor]];
    [self.view addSubview:messageLabel];
    [messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(screenScale(172));
        make.left.mas_equalTo(self.view).with.offset(screenScale(15));
        
        
    }];
    
    _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), scrHeight())];
    _bgView.backgroundColor = [UIColor blackColor];
    _bgView.alpha = 0.1;
    _bgView.hidden = YES;
    [[[UIApplication sharedApplication]keyWindow] addSubview:_bgView];
    [[[UIApplication sharedApplication]keyWindow] addSubview:self.pickerView];
    
    ///
    
    _nameTextField = [[UITextField alloc]init];
    _nameTextField.delegate = self;
    [_nameTextField addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    
    _contentTextField = [[UITextField alloc]init];
    _contentTextField.delegate = self;
    [_contentTextField addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    
    _schoolNameTextField = [[UITextField alloc]init];
    _schoolNameTextField.delegate = self;
    [_schoolNameTextField addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
}

#pragma mark ------------------ click -------------------

- (void)dateCancelBtnClick{
    
    [UIView animateWithDuration:0.5 animations:^{
        
        [UIView beginAnimations:@"move" context:nil];
        [UIView setAnimationDuration:0.75];
        [UIView setAnimationDelegate:self];
        
        _bgView.alpha = 0.1;
        self.myDatePicker.frame = CGRectMake(0, scrHeight(), scrWidth(), scrHeight() - screenScale(200) - screenScale(44 * 6));
        _dateBgView.frame = CGRectMake(0, scrHeight(), scrWidth(), scrHeight() - screenScale(200) - screenScale(44 * 5));
        
        _bgView.hidden = YES;
        
        [UIView commitAnimations];
    }];
}

- (void)dateFinishBtnClick{
    
    //获取挑选的日期
    NSDate *date =_myDatePicker.date;
    NSDateFormatter *dateForm = [[NSDateFormatter alloc]init];
    //设定转换格式
    dateForm.dateFormat =@"yyyy-MM-dd";
    //由当前获取的NSDate数据，转换为日期字符串，显示在私有成员变量_textField上
    //    _textField.text = [dateForm stringFromDate:date];
    
    [messageArray replaceObjectAtIndex:4 withObject:[dateForm stringFromDate:date]];
    
    /**
     *  单个cell的刷新
     */
    //1.当前所要刷新的cell，传入要刷新的 行数 和 组数
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:4 inSection:0];
    //2.将indexPath添加到数组
    NSArray <NSIndexPath *> *indexPathArray = @[indexPath];
    //3.传入数组，对当前cell进行刷新
    [_personTableView reloadRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic];
    
    

    [UIView animateWithDuration:0.5 animations:^{
        
        [UIView beginAnimations:@"move" context:nil];
        [UIView setAnimationDuration:0.75];
        [UIView setAnimationDelegate:self];
        
        _bgView.alpha = 0.1;
        self.myDatePicker.frame = CGRectMake(0, scrHeight(), scrWidth(), scrHeight() - screenScale(200) - screenScale(44 * 6));
        _dateBgView.frame = CGRectMake(0, scrHeight(), scrWidth(), scrHeight() - screenScale(200) - screenScale(44 * 5));
        
        _bgView.hidden = YES;
        
        [UIView commitAnimations];
    }];
    
}

- (void)textFieldEditChanged:(UITextField *)textField{
    
    [messageArray replaceObjectAtIndex:0 withObject:textField.text];
    
    NSLog(@"===%@",messageArray);
    NSLog(@"textfield text %@",textField.text);
}

- (void)rightBtnClick{
    
    NSLog(@"messageArray===\n\n%@",messageArray);
    
    if (messageArray.count != 6) {
        
        [PublicFunction showAlert:nil message:@"信息不完整" duration:1.0];
        return;
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    [productParams setValue: _nameTextField.text forKey:@"user_name"];
    [productParams setValue: messageArray[5] forKey:@"sex"];
    [productParams setValue: _contentTextField.text forKey:@"content"];
    [productParams setValue: messageArray[3] forKey:@"address"];
    [productParams setValue: messageArray[4] forKey:@"birthday"];
    [productParams setValue: _schoolNameTextField.text forKey:@"school_name"];
    
    NSLog(@"productParams===\n\n%@",productParams);
    
    //两种方式上传头像
    /*方式一：使用NSData数据流传图片*/
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         @"text/plain",
                                                         @"text/javascript",
                                                         @"text/json",
                                                         @"text/html",
                                                         @"image/jpeg", nil];
    [manager POST:more_editData parameters:productParams constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        //压缩图片
        
        if (_headimage) {
            
            //拼接图片名称+时间确保不重复命名
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            formatter.dateFormat = @"yyyyMMddHHmmss";
            NSString *imageFileName = [NSString stringWithFormat:@"%@.jpg", [formatter stringFromDate:[NSDate date]]];
            // 测试输出文件名
            
            //上传图片，以文件流的格式sdf
            
            [formData appendPartWithFileData:UIImageJPEGRepresentation(_headimage, 5.0) name:@"header_img" fileName:imageFileName mimeType:@"image/jpeg"];
            
        }
        
        if (_bgimage) {
            
            //拼接图片名称+时间确保不重复命名
            NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
            formatter1.dateFormat = @"yyyyMMddHHmmss";
            NSString *imageFileName1 = [NSString stringWithFormat:@"%@.jpg", [formatter1 stringFromDate:[NSDate date]]];
            // 测试输出文件名
            
            //上传图片，以文件流的格式sdf
            
            [formData appendPartWithFileData:UIImageJPEGRepresentation(_bgimage, 5.0) name:@"back_img" fileName:imageFileName1 mimeType:@"image/jpeg"];
            
        }
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSString *strtmp = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSString *requestTmp = [NSString stringWithString: strtmp];
        NSData *resData = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]];
        NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil];  //解析
        
        //        [self.navigationController popToRootViewControllerAnimated:YES];
        
        
        
        NSLog(@"+++>%@", resultDic);
        
        NSLog(@"--->%@", resultDic[@"msg"]);
        
        [hud removeFromSuperview];
        
//        phoneCodeViewController *vc = [[phoneCodeViewController alloc]init];
//        [self.navigationController pushViewController:vc animated:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"--->%@", error);
        
        [hud removeFromSuperview];
        
    }];
    
}

- (void)headBtnClick{

    NSLog(@"0");
    imgMark = 0;
    [self editImageSelected];
}

- (void)changeBgBtnClick{

    NSLog(@"1");
    imgMark = 1;
    [self editImageSelected];
    
}

- (void)getNum:(NSString *)sex{
    
    if ([sex isEqualToString:@"男"]) {
        
        [messageArray replaceObjectAtIndex:5 withObject:@"1"];
        
    }else{
    
        [messageArray replaceObjectAtIndex:5 withObject:@"2"];
        
    }
    
    /**
     *  单个cell的刷新
     */
    //1.当前所要刷新的cell，传入要刷新的 行数 和 组数
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:5 inSection:0];
    //2.将indexPath添加到数组
    NSArray <NSIndexPath *> *indexPathArray = @[indexPath];
    //3.传入数组，对当前cell进行刷新
    [_personTableView reloadRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - AddressPickerViewDelegate

- (void)cancelBtnClick{
    
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    _bgView.alpha = 0.1;
    [UIView commitAnimations];
    _bgView.hidden = YES;
    [self.pickerView hide];
    
}

- (void)sureBtnClickReturnProvince:(NSString *)province City:(NSString *)city Area:(NSString *)area{
    
    [messageArray replaceObjectAtIndex:3 withObject:[NSString stringWithFormat:@"%@ %@ %@",province,city,area]];
//    [_personTableView reloadData];
    [self cancelBtnClick];
    
    /**
     *  单个cell的刷新
     */
    //1.当前所要刷新的cell，传入要刷新的 行数 和 组数
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:3 inSection:0];
    //2.将indexPath添加到数组
    NSArray <NSIndexPath *> *indexPathArray = @[indexPath];
    //3.传入数组，对当前cell进行刷新
    [_personTableView reloadRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark -------------------- tableView ----------------------

#pragma mark --- UITableViewDelegate ---

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return screenScale(44);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    NSLog(@"%d",indexPath.row);
    
    if (indexPath.row == 5) {
        
        FS_MyChangrSexViewController *vc = [[FS_MyChangrSexViewController alloc]init];
        vc.delegate = self;
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (indexPath.row == 3){
    
        [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        _bgView.alpha = 0.6;
        [UIView commitAnimations];
        _bgView.hidden = NO;
        
        [_pickerView show];
        
    }else if (indexPath.row == 4){
        
        [UIView animateWithDuration:0.5 animations:^{
            
            [UIView beginAnimations:@"move" context:nil];
            [UIView setAnimationDuration:0.75];
            [UIView setAnimationDelegate:self];
            
            _bgView.alpha = 0.6;
            
            self.myDatePicker.frame = CGRectMake(0, screenScale(200) + screenScale(44 * 6), scrWidth(), scrHeight() - screenScale(200) - screenScale(44 * 6));
            
            _dateBgView.frame = CGRectMake(0, screenScale(200)+ screenScale(44 * 5), scrWidth(), scrHeight() - screenScale(200) - screenScale(44 * 5));
            
            [UIView commitAnimations];
            
        }];
    
        _bgView.hidden = NO;
        _dateBgView.hidden = NO;
        self.myDatePicker.hidden = NO;
        
    }
    
}

#pragma mark --- UITableViewDataSource ---

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView registerClass:[messageeTableViewCell class] forCellReuseIdentifier:@"identifier"];
    messageeTableViewCell *cell = (messageeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"identifier" forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.nameLable.text = titleArray[indexPath.row];
    
    cell.messageTextField.text = messageArray[indexPath.row];
    
    if (indexPath.row == 0) {
        
        cell.leftImg.hidden = YES;
        cell.messageTextField.enabled = YES;
        _nameTextField = cell.messageTextField;
        [messageArray replaceObjectAtIndex:0 withObject:cell.messageTextField.text];
        
    }else if (indexPath.row == 5){
    
        if ([messageArray[5] isEqualToString:@"1"]) {
            
            cell.messageTextField.text = @"男";
            
        }else{
        
            cell.messageTextField.text = @"女";
            
        }
        
    }else if (indexPath.row == 2){
    
        cell.leftImg.hidden = YES;
        cell.messageTextField.enabled = YES;
        _contentTextField = cell.messageTextField;
        [messageArray replaceObjectAtIndex:2 withObject:cell.messageTextField.text];
        
    }else if (indexPath.row == 1){
    
        cell.leftImg.hidden = YES;
        cell.messageTextField.enabled = YES;
        _schoolNameTextField = cell.messageTextField;
        [messageArray replaceObjectAtIndex:1 withObject:cell.messageTextField.text];
        
    }
    
    return cell;
}

#pragma mark ----------------------- 相册／相机 ---------------------

- (void)photoKitController:(STPhotoKitController *)photoKitController resultImage:(UIImage *)resultImage{
    
    if (imgMark == 0) {
        
        _headimage = resultImage;
        _headImg.image = resultImage;
        
    }else{
    
        _bgimage = resultImage;
        _bgImg.image = resultImage;
        
    }
    
    
}

#pragma mark - 2.UIImagePickerController的委托

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        UIImage *imageOriginal = [info objectForKey:UIImagePickerControllerOriginalImage];
        STPhotoKitController *photoVC = [STPhotoKitController new];
        [photoVC setDelegate:self];
        [photoVC setImageOriginal:imageOriginal];
        
        if (imgMark == 0) {
            
            [photoVC setSizeClip:CGSizeMake(_headImg.frame.size.width*4, _headImg.frame.size.width*4)];
            
        }else{
        
            [photoVC setSizeClip:CGSizeMake(_headImg.frame.size.width*8, _headImg.frame.size.width*8*0.42)];
            
        }
        
        [self presentViewController:photoVC animated:YES completion:nil];
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:^(){
    }];
}

#pragma mark - --- event response 事件相应 ---
- (void)editImageSelected
{
    grayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT)];
    grayView.backgroundColor = [UIColor colorWithRed:0 / 255.0f green:0 / 255.0f blue:0 / 255.0f alpha:0.2];
    
    [[UIApplication sharedApplication].keyWindow addSubview:grayView];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissContactView)];
    [grayView addGestureRecognizer:tapGesture];
    
    [grayView addSubview:self.changeHeadImageView];
    [self.changeHeadImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(DEF_SCREEN_WIDTH, 160));
    }];
    
    self.changeHeadImageView.layer.cornerRadius = 8;
    [self.changeHeadImageView.takePhotoButton addAction:^(NSInteger tag) {
        UIImagePickerController *controller = [UIImagePickerController imagePickerControllerWithSourceType:UIImagePickerControllerSourceTypeCamera];
        [grayView removeFromSuperview];
        if ([controller isAvailableCamera] && [controller isSupportTakingPhotos]) {
            [controller setDelegate:self];
            [self presentViewController:controller animated:YES completion:nil];
        }else {
            NSLog(@"%s %@", __FUNCTION__, @"相机权限受限");
        }
    }];
    [self.changeHeadImageView.localAlbumButton addAction:^(NSInteger tag) {
        UIImagePickerController *controller = [UIImagePickerController imagePickerControllerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [controller setDelegate:self];
        [grayView removeFromSuperview];
        if ([controller isAvailablePhotoLibrary]) {
            [self presentViewController:controller animated:YES completion:nil];
        }
    }];
    [self.changeHeadImageView.closeViewButton addAction:^(NSInteger tag) {
        [self dismissContactView];
    }];
}

- (void)dismissContactView {
    [grayView removeFromSuperview];
}

#pragma mark - ---------- 懒加载 ----------
- (ICN_HeadImageSelView *)changeHeadImageView {
    if (!_changeHeadImageView)
    {
        _changeHeadImageView = [[NSBundle mainBundle] loadNibNamed:@"ICN_HeadImageSelView" owner:self options:nil].lastObject;
    }
    return _changeHeadImageView;
}

@end
